<template>
  <div>
    <div class="main-div">
      <keep-alive>
          <router-view />
      </keep-alive>
    </div>
    <van-tabbar v-model="active" @change="changeTabbar(active)">
        <van-tabbar-item icon="shop">首页</van-tabbar-item>
        <van-tabbar-item icon="records">列表页</van-tabbar-item>
        <van-tabbar-item icon="cart">购物车页</van-tabbar-item>
        <van-tabbar-item icon="contact" info="20">会员中心</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        active: 0,
        nowPath:''
      }
    },
    created(){
      this.changeTabBarActive()
    },
    updated(){
      this.changeTabBarActive()
    },
    // watch:{
    //   $route(val){
    //     let url=val.fullPath
    //     if(url=='/Cart'){
    //       this.active=2
    //     }
    //     console.log(val,999)
    //   }
    // },
    methods:{
      // 处理从商品详情进入到购物车，下面的tabs没选中购物车
      changeTabBarActive(){
        console.log(this.$route,'path')
        this.nowPath=this.$route.path
        if(this.nowPath=='/Cart'){
          this.active=2
        }
      },
      // 切换导航
      changeTabbar(active){
        console.log(active)
        switch(active){
          case 0:
            this.$router.push({name:'shoppingMall'})
            break;
          case 1:
            this.$router.push({name:'CategoryList'})
            break;
          case 2:
            this.$router.push({name:'Cart'})
            break;
          case 3:
            this.$router.push({name:'member'})
            break;
        }
      }
    }
  }
</script>

<style scoped>

</style>
